﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using OpenPhotoAlbum.DbAccess;

namespace OpenPhotoAlbum.BusinessLogics
{
    public class ActionManager
    {
        #region Singleton

        private static readonly ActionManager _instance = new ActionManager();

        static ActionManager() { }

        private ActionManager() { }

        public static ActionManager Instance { get { return _instance; } }

        #endregion

        private void Add(DataClassesUADataContext context, string type, string data1, string data2, string data3)
        {
            DbAccess.Action action = new DbAccess.Action();
			action.Id = Guid.NewGuid();
            action.IpAddress = HttpContext.Current.Request.UserHostAddress;
            action.ActionType = type;
            action.ActionData1 = data1;
            action.ActionData2 = data2;
            action.ActionData3 = data3;
            action.ActionTime = DateTime.UtcNow;

            context.Actions.InsertOnSubmit(action);
        }

        private void Add(string type, string data1, string data2, string data3)
        {
            DataClassesUADataContext context = new DataClassesUADataContext();
            Add(context, type, data1, data2, data3);
            context.SubmitChanges();
        }

        public void AddLoginSuccess(string userName)
        {
            Add(ActionTypes.LoginSuccess, userName, null, null);
        }

        public void AddLoginFailure(string badUserName)
        {
            Add(ActionTypes.LoginFailure, badUserName, null, null);
        }

        public void AddPhotoUpload(Guid photoId, string photoTitle)
        {
            Add(ActionTypes.PhotoUpload, photoId.ToString(), photoTitle, null);
        }

        public void AddPhotoTitleChange(Guid photoId, string photoTitle)
        {
            //Add(ActionTypes.PhotoTitleChange, photoId.ToString(), photoTitle, null);
        }

        public void AddPhotoDescriptionChange(Guid photoId, string photoDescription)
        {
            //Add(ActionTypes.PhotoDescriptionChange, photoId.ToString(), photoDescription, null);
        }

        public void AddPasswordChange(string userName)
        {
            //Add(ActionTypes.PasswordChange, userName, null, null);
        }

        public void AddEmailChange(string userName, string newEmail)
        {
            Add(ActionTypes.EmailChange, userName, newEmail, null);
        }

        public void AddComment(string comment, Guid photoId)
        {
            Add(ActionTypes.Comment, comment, photoId.ToString(), null);
        }

        public void AddUserRegisteration(string userName)
        {
            Add(ActionTypes.UserRegisteration, userName, null, null);
        }

		public void AddPasswordRecoveryEmailSent(string userName, string email)
		{
			Add(ActionTypes.PasswordRecoveryEmailSent, userName, email, null);
		}

		public void AddApplicationError(Exception exception, string url)
		{
			string message = string.Empty;
			string innerMessage = string.Empty;
			if (url == null) { url = string.Empty; }

			if (exception != null)
			{
				if (!string.IsNullOrEmpty(exception.Message))
					message = exception.Message;

				if (exception.InnerException != null)
				{
					if (!string.IsNullOrEmpty(exception.InnerException.Message))
						innerMessage = exception.InnerException.Message;
				}
			}

			Add(ActionTypes.ApplicationError, message, innerMessage, url);
		}

        public void Delete(DateTime timeline)
        {
            DataClassesUADataContext context = new DataClassesUADataContext();
            var results = from a in context.Actions
                          where a.ActionTime.CompareTo(timeline) < 0
                          select a;
            context.Actions.DeleteAllOnSubmit(results);
            context.SubmitChanges();
        }
    }

    public class ActionTypes
    {
        public const string LoginSuccess = "LoginSuccess";
        public const string LoginFailure = "LoginFailure";
        public const string PhotoUpload = "PhotoUpload";
        public const string PhotoTitleChange = "PhotoTitleChange";
        public const string PhotoDescriptionChange = "PhotoDescriptionChange";
        public const string PasswordChange = "PwdChange";
        public const string EmailChange = "EmailChange";
        public const string Comment = "Comment";
        public const string UserRegisteration = "UserReg";
		public const string PasswordRecoveryEmailSent = "PwdRecoveryEmailSent";
		public const string ApplicationError = "AppError";
    }
}
